<?php

namespace App\Admin\Controllers;

use App\Models\ProductsModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ProductsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'ProductsModel';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ProductsModel());
        $grid->column('id', __('Id'))->display(function ($id){
            $id=$this->id;
            return "<span style='line-height: 100px'>{$id}</span>";
        });
        $grid->column('p_name', __('商品名'))->display(function ($p_name){
            $p_name=$this->p_name;
            return "<span style='line-height: 100px'>{$p_name}</span>";
        });
        $grid->column("p_img",__('商品图片'))->image();
        $grid->column('price', __('价格'))->display(function ($price){
            $price=$this->price;
            return "<span style='line-height: 100px'>{$price}</span>";
        });
        $grid->column('inventory', __('库存'))->display(function ($inventory){
            $inventory=$this->inventory;
            return "<span style='line-height: 100px'>{$inventory}</span>";
        });
        $grid->column('created_at', __('创建时间'))->display(function ($created_at){
            $created_at=$this->created_at;
            return "<span style='line-height: 100px'>{$created_at}</span>";
        });
        $grid->column('updated_at', __('修改时间'))->display(function ($updated_at){
            $updated_at=$this->updated_at;
            return "<span style='line-height: 100px'>{$updated_at}</span>";
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ProductsModel::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('p_name', __('P name'));
        $show->field('price', __('Price'));
        $show->field('inventory', __('库存'));
        $show->field("p_img",__('商品图片'))->image();
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ProductsModel());

        $form->text('p_name', __('商品名'));
        $form->image("p_img",__('商品图片'));
        $form->number('price', __('价格'));
        $form->number('inventory', __('库存'));
        return $form;
    }
}
